package com.woniuxy.main.pay.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.woniuxy.main.pay.dao.OrderCommentMapper;
import com.woniuxy.main.pay.dao.OrderMapper;
import com.woniuxy.main.pay.pojo.Order;
import com.woniuxy.main.pay.pojo.OrderComment;
import com.woniuxy.main.pay.pojo.OrderTypeCount;
import com.woniuxy.main.pay.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderCommentMapper commentMapper;
	
	@Override
	public List<Order> allOrderService(Integer userId, Integer currentPage, Integer pageSize, Integer orderShowState,Integer role) {
		List<Order> orderSelect=new ArrayList<Order>();
		List<Order> orderShow=new ArrayList<Order>();
		List<Order> orderShow2=new ArrayList<Order>();
		String cName=null,vName=null;
		int cId,vId;
		PageInfo<Order> info=null;
		if (role==1) {
			Integer studentOrderId=orderMapper.searchOrderStudentId(userId);
			if(orderShowState<1){
				orderSelect = orderMapper.allOrderShowMapp(studentOrderId, currentPage, pageSize);
				info = new PageInfo<Order>(orderSelect);
				orderShow = info.getList();
			}else {
				orderSelect = orderMapper.nonOrderShowMapp(studentOrderId, orderShowState,currentPage, pageSize);
				info = new PageInfo<Order>(orderSelect);
				orderShow = info.getList();
			}
		}else if(role==2){
			Integer coachOrderId=orderMapper.searchOrderCoachId(userId);
			if(orderShowState<1){
				orderSelect = orderMapper.allOrderShowMapp2(coachOrderId, currentPage, pageSize);
				info = new PageInfo<Order>(orderSelect);
				orderShow = info.getList();
			}else {
				orderSelect = orderMapper.nonOrderShowMapp2(coachOrderId, orderShowState,currentPage, pageSize);
				info = new PageInfo<Order>(orderSelect);
				orderShow = info.getList();
			}
		}
		
		for (Order order : orderShow) {
			cId=orderMapper.searchCoachId(order.getCoachId());
			vId=orderMapper.searVenueId(order.getVenueId());
			cName=orderMapper.searchNickName(cId);
			vName=orderMapper.searchNickName(vId);
			order.setCoachName(cName);
			order.setVenueName(vName);
			String user_headimg=orderMapper.searUserHeadimg(cId);
//			System.out.println("头像地址"+user_headimg);
			order.setUserHeadimg(user_headimg);
			orderShow2.add(order);
		}
		return orderShow2;
	}

	@Override
	public OrderTypeCount orderTC(Integer userId,Integer userRole) {
		OrderTypeCount otc=new OrderTypeCount();
		Integer allOrder,obligation,okOrder,noComment,cancel;
		if (userRole==1) {
			Integer studentOrderId=orderMapper.searchOrderStudentId(userId);
			allOrder=orderMapper.searchAllCount(studentOrderId);
			obligation=orderMapper.searchObligation(studentOrderId,3);
			noComment=orderMapper.searchObligation(studentOrderId,4);
			cancel=orderMapper.searchObligation(studentOrderId,1);
			okOrder=orderMapper.searchObligation(studentOrderId,6);
			otc.setCancel(cancel);
			otc.setNoComment(noComment);
			otc.setObligation(obligation);
			otc.setOkOrder(okOrder);
			otc.setAllOrder(allOrder);
		}else if (userRole==2) {
			Integer coachOrderId=orderMapper.searchOrderCoachId(userId);
			allOrder=orderMapper.searchAllCount2(coachOrderId);
			obligation=orderMapper.searchObligation2(coachOrderId,3);
			noComment=orderMapper.searchObligation2(coachOrderId,4);
			cancel=orderMapper.searchObligation2(coachOrderId,1);
			okOrder=orderMapper.searchObligation2(coachOrderId,6);
			otc.setCancel(cancel);
			otc.setNoComment(noComment);
			otc.setObligation(obligation);
			otc.setOkOrder(okOrder);
			otc.setAllOrder(allOrder);
		}
		return otc;
	}
	@Override
	public Integer searchUserRole(Integer userId) {
		Integer role=orderMapper.searchRole(userId);
		return role;
	}

	@Override
	public Order detailService(Integer orderId) {
		Order orderShow=new Order();
		orderShow=orderMapper.detailMaooer(orderId);
		String cName=null,vName=null,sName=null;
		String sComment;
		int cId,vId,sId;
		sComment=orderMapper.studentComment(orderShow.getOrderId());
//		cComment=orderMapper.coachComment(orderShow.getOrderId());
//		orderShow.setCoachComment(cComment);
		orderShow.setStudentComment(sComment);
		cId=orderMapper.searchCoachId(orderShow.getCoachId());
		vId=orderMapper.searVenueId(orderShow.getVenueId());
		sId=orderMapper.searStudentId(orderShow.getStudentId());
		sName=orderMapper.searchNickName(sId);
		cName=orderMapper.searchNickName(cId);
		vName=orderMapper.searchNickName(vId);
		orderShow.setCoachName(cName);
		orderShow.setVenueName(vName);
		orderShow.setStudentName(sName);
		String user_headimg=orderMapper.searUserHeadimg(cId);
		orderShow.setUserHeadimg(user_headimg);
//		System.out.println("打桩评论:"+sComment);
		return orderShow;
	}

	@Override
	public List<Order> searchOrder(Integer userId, int i) {
		List<Order> orderSelect=new ArrayList<Order>();
		Integer sutdentId=orderMapper.searchOrderStudentId(userId);
		orderSelect = orderMapper.searchOrderMapp(sutdentId);
		return orderSelect;
	}
	
	@Override
	public List<Order> searchCoachOrder(Integer coachId) {
		List<Order> orderSelect=new ArrayList<Order>();
		orderSelect = orderMapper.searchCoachOrderMapp(coachId);
		return orderSelect;
	}

	@Override
	public Integer searchStudentId(Integer userId) {
		Integer studentId=orderMapper.searStudentId(userId);
		return studentId;
	}
	
	@Override
	public void addOrder(Order addOrder) {
		orderMapper.addOrder(addOrder);
	}
	@Override
	public void minus(Integer studentId,Integer money) {
		Integer studentBalance=orderMapper.searchBalance(studentId);//学生余额
		orderMapper.addBalance(studentId, (studentBalance-money));
	}

	@Override
	public void updateState(Integer orderId, Integer stateId) {
		orderMapper.updateStateMaper(orderId,stateId);
	}
	@Override
	public void updateStateTime(Integer orderId, Integer stateId, String courseTimeNew) {
		Integer durationTime=orderMapper.coachDurationMaper(orderId);
		
		long etime1=System.currentTimeMillis()+durationTime*60*1000;//延时函数，单位毫秒
		SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String order_complete=time2.format(new Date(etime1));
		
		Order orderState =new Order();
		orderState.setOrderId(orderId);
		orderState.setOrderState(stateId);
		orderState.setCourseTime(courseTimeNew);
		orderState.setOrderComplete(order_complete);
		orderMapper.updateStateTimeMaper(orderState);
	}

	@Override
	public void updateState2(Order upOrder) {
		orderMapper.updateStateMaper2(upOrder);
	}

	@Override
	public String coachTime(Integer orderId) {
		String result=null;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String newTime=sdf.format(new Date());
		String oldTime=orderMapper.coachTimeMaper(orderId);
		Integer oldDuration=orderMapper.coachDurationMaper(orderId);
		int a2=Integer.parseInt(oldTime.substring(11, 13));//小时
		int a3=Integer.parseInt(oldTime.substring(14, 16));//分钟
		int b2=Integer.parseInt(newTime.substring(11, 13));//小时
		int b3=Integer.parseInt(newTime.substring(14, 16));//分钟
		int aa=(a2*60)+(a3)+oldDuration;//结束时间
		int bb=(b2*60)+(b3);//当前时间
		if(aa>=bb){
			result="还有"+(aa-bb)+"分钟才结束;是否结束课程?";
		}else if(bb>aa){
			result="已超出预定时长:"+(bb-aa)+"分钟;课程结束";
		}
		return result;
	}

	@Override
	public String upOrderState3(Integer orderId,Integer stateId) {
		orderMapper.updateStateMaper(orderId,stateId);
		Order orderShow=new Order();
		orderShow=orderMapper.detailMaooer(orderId);
		Integer coachId=orderMapper.searchCoachId(orderShow.getCoachId());
		Integer coachBalance=orderMapper.searchBalance(coachId);//教练余额
		orderMapper.addBalance(coachId, (coachBalance+orderShow.getOrderMoney()));
		System.out.println("打桩"+(coachBalance+orderShow.getOrderMoney())+"教练USERid："+coachId);
		
		return "付款成功";
	}

	@Override
	public void addComment(OrderComment comments) {
		orderMapper.addCommentMapper(comments);
		orderMapper.updateStateMaper(comments.getOrder_id(),6);
	}

	@Override
	public String cancel(Integer orderId, Integer stateId) {
		orderMapper.updateStateMaper(orderId,stateId);
		
		return "订单已取消";
	}



//	@Override
//	public String selectComment(Integer orderId) {
//		String comment=orderMapper.selectComentMap(orderId);
//		return comment;
//	}

}
